﻿'use strict';
app
    // Dashboard Box controller 
    .controller('WalletHistoryController', ['$rootScope', '$scope', '$localStorage', '$http', '$stateParams', '$modal', function ($rootScope, $scope, $localStorage, $http, $stateParams, $modal) {

        $scope.NameWallet = "", $scope.Balance = "",
        $scope.popup = function (windowClass, StyleModal, Controller, size) {
            $modal.open({
                windowClass: windowClass,
                templateUrl: StyleModal,
                controller: Controller,
                size: size,
                resolve: {
                    messeger: function () {
                        return $scope.messeger;
                    },
                    title: function () {
                        return $scope.title;
                    }
                }
            })
        },
        $http.post($rootScope.APITransactionUrl + 'GetWallets', { UserId: $localStorage.user.UserId }).then(function successCallback(resp) {
            var Wallets = resp.data;
            if (resp.data) {
                Wallets.forEach(function (item) {
                    if (item.Id == 1) {
                        $scope.NameWallet = item.Name; 
                        $scope.Balance = item.Balance;
                    }
                })
            }           
        }, function errorCallback(resp) {

        }),
        $http.post($rootScope.APITransactionUrl + 'GetWalletsHistory', { UserId: $localStorage.user.UserId, WalletId: $stateParams.WalletId }).then(function successCallback(resp) {
            $scope.Walletlists = resp.data;
        }, function errorCallback(resp) {
            
        })

    }
    ]);
app.controller('WalletModalInstanceCtrl', function ($scope, $modalInstance, $localStorage, messeger, title) {
    $scope.messeger = messeger,
    $scope.title = title;
    $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});
app.controller('WalletCashoutModalInstanceCtrl', function ($scope, $modalInstance, $localStorage, $http, $rootScope, $timeout, $state, $stateParams, messeger, title, commonFactory, WalletFactory) {

    $scope.messeger = messeger,
    $scope.title = title,
    $scope.processing = false,
    $scope.AlertType = "warning";
    $scope.payment = {},
    $scope.wallet = {},
    $scope.PaymentMethod = [],
    $scope.selected = [],
    $scope.FeeConfig = {},
    $scope.data = { Amount: "", Fee: 0, After: 0 };
    //get fee cashout dynamit
    $scope.doProcess = function (data) {
        $scope.messeger = "";
        if (parseFloat(data.Amount) > 0.025) {
            $scope.data.After = data.Amount - (data.Amount * data.Fee);
        } else {
            $scope.data.After = 0;
            $scope.messeger = "Amount must greater > 0.025";
        }
    },
    $scope.doCashout = function (item) {
        $scope.processing = true,
        $scope.ExistSession = false,
        $scope.messeger = "";
        if (item.Amount > 0 && item.PaymentId > 0) {
            commonFactory.CheckSession($localStorage.user.SessionId).then(function (resp) {
                if (resp.data.Status) {
                    //do prosess
                    Cashout(item)
                } else {
                    $localStorage.user = null;
                    $scope.messeger = "Session is expired";
                    $timeout(function () {
                        $state.go('login');
                    }, 5000);
                }
            }, function (error) {
                $scope.status = 'Unable to check session data: ' + error.message;
            });

        } else {
            $scope.processing = false,
            $scope.messeger = "Error: Amount >= 0,";
        }
    },
    GetPaymentMethod(),
    getFeeConfig(),
    NotFundingWalletList($localStorage.user.UserId);
    $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
    //check session   
    function NotFundingWalletList(UserId) {
        commonFactory.GetNotFundingWalletList(UserId).then(function (response) {
            $scope.WalletList = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    }
    function GetPaymentMethod() {
        commonFactory.GetPaymentMethod().then(function (response) {
            $scope.PaymentMethod = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    }
    function getFeeConfig() {
        WalletFactory.getFeeConfig().then(function (response) {
            $scope.data.Fee = response.data.Result.Cashout;
        }, function (error) {
            $scope.status = 'Unable to load profile data: ' + error.message;
        });
    }

    function Cashout(item) {
        $http.post($rootScope.APITransactionUrl + 'Cashout', {
            userid: $localStorage.user.UserId,
            amount: item.Amount,
            payment: item.PaymentId,
            walletId: item.Wallet
        }).then(function successCallback(resp) {
            $scope.processing = false;
            if (resp.data.Status) {
                $scope.AlertType = "success";
                $state.transitionTo($state.current, $stateParams, {
                    reload: true,
                    inherit: false,
                    notify: true
                }),
                $timeout(function () {
                    $modalInstance.close();
                }, 5000);
            } else {
                $scope.AlertType = "warning";
            }
            $scope.messeger = resp.data.Messeger;
        })
    }

});